﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace RMS.DB
{
    class SoldItemsMng : DBController
    {
        public List<SoldItem> GetAllItems()
        {
            var listItems = new List<SoldItem>();
            try
            {
                IOrderedQueryable<SoldItem> query = from item in DataEntities.SoldItems
                                                    orderby item.ExportDate
                                                    select item;

                listItems = query.ToList();
            }
            catch (Exception ex)
            {
                MessageBox.Show(Constants.Msg.ConnectError);
            }

            return listItems;
        }

        public List<SoldItem> GetListItems(string orderCode)
        {
            var listItems = new List<SoldItem>();
            try
            {
                IQueryable<SoldItem> query = from item in DataEntities.SoldItems
                                             where item.OrderCode.Equals(orderCode)
                                             select item;

                listItems = query.ToList();
            }
            catch (Exception ex)
            {
                MessageBox.Show(Constants.Msg.ConnectError);
            }

            return listItems;
        }

        public List<SoldItem> GetListItemsByCustomerId(string cusId)
        {
            var listItems = new List<SoldItem>();
            try
            {
                IQueryable<SoldItem> query = from item in DataEntities.SoldItems
                                             where item.CustomerID.Equals(cusId) && item.SaleInvoiceNo != "isdraft"
                                             orderby item.ExportDate descending
                                             select item;

                listItems = query.ToList();
            }
            catch (Exception ex)
            {
                MessageBox.Show(Constants.Msg.ConnectError);
            }

            return listItems;
        }

        public SoldItem GetItems(string id, string orderCode)
        {
            var item = new SoldItem();
            try
            {
                IQueryable<SoldItem> query = from _item in DataEntities.SoldItems
                                             where _item.ProductID.Equals(id) && _item.OrderCode.Equals(orderCode)
                                             select _item;

                item = query.FirstOrDefault();
            }
            catch (Exception ex)
            {
                MessageBox.Show(Constants.Msg.ConnectError);
            }

            return item;
        }
        public bool UpdateItem(string orderCode, decimal importPrice, int exportQuantity, decimal exportPrice, decimal exportWeight, DateTime exportDate, string customerId, string saleInvoiceNo)
        {
            try
            {
                var items = new List<SoldItem>();

                IQueryable<SoldItem> query = from _item in DataEntities.SoldItems
                                             where _item.OrderCode.Equals(orderCode)
                                             select _item;

                items = query.ToList();

                if (items.Count == 0)
                {
                    return false;
                }

                DataEntities.SaveChanges();
                return true;
            }
            catch (Exception)
            {
                MessageBox.Show(Constants.Msg.ConnectError);
                return false;
            }
        }

        public bool AddNewSoldItems(string productId, string name, string productType, string orderCode, decimal importPrice, int importQuantity, int exportQuantity, decimal exportPrice, decimal exportWeight, DateTime exportDate, string customerId, string saleInvoiceNo)
        {
            if (string.IsNullOrEmpty(productId))
            {
                MessageBox.Show(Constants.Msg.FieldRequired);
                return false;
            }
            try
            {
                var item = new SoldItem { ProductID = productId,
                Name = name,
                ProductType = productType,
                OrderCode = orderCode,
                ImportPrice = importPrice,
                ImportQuantity = importQuantity,
                ExportDate = exportDate,
                ExportPrice = exportPrice,
                ExportQuantity = exportQuantity,
                ExportWeight = exportWeight,
                CustomerID = customerId,
                SaleInvoiceNo = saleInvoiceNo };
                DataEntities.AddToSoldItems(item);
                DataEntities.SaveChanges();
                return true;
            }
            catch (Exception)
            {
                MessageBox.Show(Constants.Msg.ConnectError);
                return false;
            }
        }
        public void DeleteItem(string id, string orderCode)
        {
            try
            {
                var item = new SoldItem();
                item = GetItems(id, orderCode);
                if (item == null)
                    return;
                DataEntities.DeleteObject(item);
                DataEntities.SaveChanges();
            }
            catch (Exception ex)
            {
                MessageBox.Show(Constants.Msg.ConnectError);
            }
        }
    }
}
